/*!
 * Fresns v1.0.0 (https://fresns.cn/)
 * Copyright 2020 Jarvis Tang (https://tangjie.me)
 * Licensed under Apache License 2.0 (https://gitee.com/Fresns/Docs/LICENSE)
*/
/*!
 * Plugins
 * - OverlayScrollbars
 * - Select2
 * - Select2-Bootstrap4-Theme
 * - Moment
 * - Date Range Picker
 */

// plugin: OverlayScrollbars
// page: options-general
// page: options-post-expand
// page: options-channel
// page: content-categories
// page: client-Fresns
// page: client-discuzx
$(function () {
    $('textarea').overlayScrollbars({
        className: 'textarea_wrapper',
        textarea: {
            dynWidth: false,
            dynHeight: true
        },
    });
});

// plugin: select2
// plugin: select2-bootstrap4-theme
// page: user-manage
// page: options-post-expand
// page: content-categories
$(function () {
    $('select').select2({
        theme: 'bootstrap4',
    });
});

// plugin: Moment
// plugin: Date Range Picker
// page: wallet-record
$(function () {
    moment.locale('zh-cn');
    $('input[name="datefilter"]').daterangepicker({
        "showDropdowns": true,
        "minYear": 2020,
        "autoApply": true,
        "maxSpan": {
            "days": 90
        },
        "maxDate": new Date(),
        "opens": "left"
    }, function (start, end, label) {
        console.log('New date range selected: ' + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD') + ' (predefined range: ' + label + ')');
    });
});

// page: options-send
// page: options-post
$(document).ready(function () {
    $.typeView = {
        'container': $('#container'),
        'phpmail': $('#phpmail'),
        'sendmail': $('#sendmail'),
        'smtp': $('#smtp'),
        'date': $('#date'),
        'datetime': $('#datetime'),
        'time': $('#time')
    };
    $('#email_send_mode').change(function () {
        // hide all
        $.each($.typeView, function () { this.hide(); });
        // show current
        $.typeView[$(this).val()].show();
    }).change();
    $('#content_post_rule').change(function () {
        // hide all
        $.each($.typeView, function () { this.hide(); });
        // show current
        $.typeView[$(this).val()].show();
    }).change();
});

// page: options-post-expand
$(function () {
    $('[data-toggle="tooltip"]').tooltip()
});

// page: wallet-review
$(function () {
    $('[data-toggle="popover"]').popover()
});

// page: content-categories
$(function () {
    $('.categories-collapse').click(function() { 
        $(this).find('i').toggleClass('fas fa-plus fas fa-minus'); 
    });
});

// page: options-account
var elem = document.getElementById("captcha_service");
elem.onchange = function () {
    var hiddenDiv = document.getElementById("captcha_input");
    hiddenDiv.style.display = (this.value == "") ? "none" : "block";
};
